package com.ufo.store.management.ui.fragment;


import com.google.zxing.WriterException;
import com.ufo.store.management.ui.activity.PasswordFindActivity;
import com.ufo.store.management.ui.activity.SettingActivity;
import com.ufo.store.management.R;
import com.ufo.store.management.utils.AnimationUtil;
import com.ufo.store.management.utils.DialogUtil;
import com.ufo.store.management.utils.StatusBarUtil;
import com.ufo.store.management.utils.Utils;
import com.ufo.store.management.widget.dialog.InputDialog;
import com.ufo.store.management.widget.zxing.encode.CodeCreator;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class AccountFragment extends BaseLazyFragment {
	public static final String TAG = AccountFragment.class.getSimpleName();
	private ImageView mRightIv = null;
	private ImageView mCodeIv = null;
	private InputDialog mInputDialog = null;
	/**
	 * 布局文件ID
	 *
	 * @return
	 */
	protected int getLayoutId(){
		return R.layout.fragment_account;
	}

	/**
	 * 初始化组件
	 */
	protected void initViewData(View entryView){
		mRightIv  = (ImageView)entryView.findViewById(R.id.tv_right);
		mCodeIv = (ImageView)entryView.findViewById(R.id.iv_code);

		mRightIv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent toIntent = new Intent(getActivity(), SettingActivity.class);
				startActivity(toIntent);
			}
		});

		entryView.findViewById(R.id.rl1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent toIntent = new Intent(getActivity(), SettingActivity.class);
				startActivity(toIntent);
			}
		});
		entryView.findViewById(R.id.rl2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				showCopyrightInputDialog();
			}
		});

		entryView.findViewById(R.id.rl3).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent toIntent = new Intent(getActivity(), PasswordFindActivity.class);
				startActivity(toIntent);
			}
		});
		try {
			Bitmap qrCodeBitmap = CodeCreator.createQRCode("Store");
			mCodeIv.setImageBitmap(qrCodeBitmap);
		} catch (WriterException e) {
			e.printStackTrace();
		}


		RelativeLayout content_ll = (RelativeLayout)entryView.findViewById(R.id.content_rl);
		AnimationUtil.setTopToBottomAnimation(content_ll,500,0.1f);
	}

	/**
	 * 初始化数据
	 */
	protected void initData(){
		loaded();
	}

	protected void onFirstUserVisible(){
		StatusBarUtil.StatusBarLightMode(getActivity(), R.color.transparent, false);
	}

	protected void onUserVisible(){
		StatusBarUtil.StatusBarLightMode(getActivity(), R.color.transparent, false);
	}

	protected void onFirstUserInvisible(){

	}
	protected void onUserInvisible(){

	}

	public void showCopyrightInputDialog( ) {
		if (mInputDialog == null) {
			mInputDialog = new InputDialog(getActivity());
			Window window = mInputDialog.getWindow(); //得到对话框
			window.setWindowAnimations(R.style.dialog_anim_center_style); //设置窗口弹出动画
		}
		mInputDialog.setHint("输入手机IMEI码以授权使用");
		mInputDialog.setItemClickListener(new InputDialog.OnItemClick() {
			@Override
			public void onItemClick(int position, String input) {
				if (position == 0) {

				}else if( position == 1 ){
					String ss = Utils.MD5(input).substring(0,6);
					showTipsDialog(input + "的授权码为：" + ss);
				}
			}
		});
		mInputDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
			}
		});
		mInputDialog.setCancelable(false);
		mInputDialog.show();
	}

	public void showTipsDialog( final String tips) {
		DialogUtil.showTipsDialog(getActivity(), tips, new Runnable() {
			@Override
			public void run() {
			}
		});
	}
}
